//
//  HP_RecommendListTableViewCell.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/9/1.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "HP_RecommendListTableViewCell.h"

@implementation HP_RecommendListTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

-(void)initialize{
    _iconImageView = [[UIImageView alloc]init];
    _iconImageView.image = [UIImage imageNamed:@"Group"];
    [self addSubview:_iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10);
        make.top.mas_equalTo(self).offset(17);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-17);
        make.width.mas_equalTo(self.iconImageView.mas_height);
    }];
    
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.text = @"Apple iphoneX国行正品 4G智能手机";
    _nameLabel.textColor = HexColor(0x454545);
    _nameLabel.font = UIFontMaoNormal;
    [self addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(10);
        make.top.mas_equalTo(self.iconImageView);
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.height.mas_offset(@14);
    }];
    
    _distanceLabel = [[UILabel alloc]init];
    _distanceLabel.text = @"100m以内";
    _distanceLabel.font = [UIFont systemFontOfSize:12];
    _distanceLabel.textColor = HexColor(0x666666);
    _distanceLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_distanceLabel];
    [_distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.iconImageView);
        make.width.mas_offset(@130);
    }];
    
    _instalmentLabel = [[PriceView alloc]init];
    [_instalmentLabel updateInfoPrice:@"458.25" periods:@"12" priceShowType:PriceShowTypeGrayPeriods];
    [self addSubview:_instalmentLabel];
    [_instalmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.iconImageView);
        make.right.mas_equalTo(self.distanceLabel.mas_left);
    }];
    
    _priceLabel = [[UILabel alloc]init];
    _priceLabel.textColor = ThemeMaoBlackColor;
    _priceLabel.text = @"￥7599";
    _priceLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.instalmentLabel.mas_top).offset(-15);
        make.right.mas_equalTo(self.nameLabel);
        
    }];
    
    _configurationView = [[UIView alloc]init];
    _configurationView.backgroundColor = [UIColor whiteColor];
    [self creatLabel:3 view:self.configurationView array:@[@"全面屏",@"面部解锁",@"黑科技慢慢"]];
    [self addSubview:_configurationView];
    [_configurationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10);
        make.height.mas_offset(@30);
    }];
    
    
}

-(void)creatLabel:(NSInteger)count view:(UIView *)view array:(NSArray *)array{
    

    CGFloat currentY = 0;
    CGFloat currentX = 0;
    
    
    CGFloat forwardHeight = 0;
    //间距
    CGFloat labelSpace = 3;
    //字体大小
    CGFloat fontFloat = 10;
    
    for (int i=0; i<count; i++) {
        NSString *title = array[i];
        UILabel *label = [[UILabel alloc]init];
        label.text = title;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = HexColor(0x999999);
        label.layer.masksToBounds = YES;
        label.layer.borderColor = HexColor(0xCCCCCC).CGColor;
        label.layer.borderWidth = .5f;
        label.layer.cornerRadius = 2;
        label.font = [UIFont systemFontOfSize:fontFloat];
        [view addSubview:label];
        
        CGSize size = [self boundingRectWithSize:array[array.count -count + i] Font:[UIFont systemFontOfSize:fontFloat] Size:CGSizeMake(ScreenWidthMao - 40, 40)];
        if (currentX + size.width >ScreenWidthMao - 10) {
            currentX = labelSpace;
            currentY = currentY + forwardHeight + 10;
        }
        label.frame = CGRectMake(currentX, currentY, size.width, 15);
        [view addSubview:label];
        forwardHeight = 25;
        currentX = currentX + size.width + 8;
        
    }
}

- (CGSize) boundingRectWithSize:(NSString*)string Font:(UIFont*) font Size:(CGSize) size{
    CGSize _size;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    NSDictionary *attribute = @{NSFontAttributeName: font};
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
    NSStringDrawingUsesLineFragmentOrigin |
    NSStringDrawingUsesFontLeading;
    _size = [string boundingRectWithSize:size options: options attributes:attribute context:nil].size;
#else
    _size = [string sizeWithFont:font constrainedToSize:size];
#endif
    _size.height += 10;
    _size.width += 10;
    return _size;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
